package com.haust.community.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: csp1999
 * @Date: 2020/11/24/21:08
 * @Description: Cookie工具类
 */
public class CookieUtil {

    /**
     * 通过 request 获取 cookie 中的数据
     * @param request
     * @param name
     * @return
     */
    public static String getValue(HttpServletRequest request, String name) {
        if (request == null || name == null) {
            throw new IllegalArgumentException("参数为空!");
        }
        // 获取cookie数组
        Cookie[] cookies = request.getCookies();
        // 遍历cookie数组,并从中取出和参数name匹配的cookie值
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
}
